<?php
    /**
     * Votes
     *
     * The Votes class vote manager
     *
     * @copyright Copyright (c) 2011,  Progect
     */         
    class Votes  implements iJSingleton{              
        /**
         *
         * This function is class construct
         *
         * @access public
         */           
        public function __construct() {

        }
        
        public function addVote($charName, $serverId, $prefix, $estimate, $countVotes = 1) {
         $iJDataBase  =  iJFactory::singleton('iJDataBase');
         $QPage = $iJDataBase->query('INSERT INTO :table_votes
                                                    (                                                     
                                                        date,
                                                        char_name,
                                                        server_id,
                                                        estimate,
                                                        count_votes                                                                                                                
                                                    )
                                                    VALUES 
                                                    (
                                                         NOW(),
                                                        :char_name,
                                                        :server_id,
                                                        :estimate,
                                                        :count_votes                                                                                                             
                                                    )                                      
                                      ');
          $QPage->bindTable(':table_votes',   TABLE_VOTES);          
          $QPage->bindValue(':char_name',     $prefix . '/' . $charName);
          $QPage->bindValue(':server_id',     $serverId);
          $QPage->bindValue(':estimate',      $estimate);
          $QPage->bindValue(':count_votes',   $countVotes);          
          $QPage->execute(); 
        }
        public function addVoteForServer(){
          $iJDataBase  =  iJFactory::singleton('iJDataBase');
          $iJDataBase->startTransaction();
          $QSelect = $iJDataBase->query('SELECT
                                          UNIX_TIMESTAMP(date) AS d,
                                          char_name,
                                          server_id,
                                          estimate,
                                          count_votes
                                        FROM :table_votes
                                        ');
          $QSelect->bindTable(':table_votes', TABLE_VOTES);          
          $QSelect->execute();
          
          if($iJDataBase->isError()){
            $iJDataBase->rollbackTransaction();
            iJDebug::setError('CRITICAL ERROR: SELECT FROM ' . TABLE_VOTES);
            return;
          }
                                                       
         while($QSelect->next()){
            $date         = $QSelect->value('d');
            $str          = $QSelect->value('char_name');            
            $mas          = split('/',$str);
            $charName     = $mas[1]; 
            $prefix       = $mas[0];
            $serverId     = $QSelect->value('server_id');
            $estimate     = $QSelect->value('estimate');
            $countVotes   = $QSelect->value('count_votes');
                        
            $QUpdate      = $iJDataBase->query('UPDATE :table_gameservers 
                                                SET votes = votes + :countVotes, 
                                                    estimate = (estimate * votes + :countVotes * :estimate)/(votes + :countVotes) 
                                                WHERE id = :server_id
                                              ');
                                                          
            $QUpdate->bindTable(':table_gameservers', TABLE_GAME_SERVERS);
            $QUpdate->bindValue(':server_id',     $serverId);
            $QUpdate->bindValue(':estimate',      $estimate);
            $QUpdate->bindValue(':countVotes',    $countVotes);
            $QUpdate->execute();
            
            if($iJDataBase->isError()){
              $iJDataBase->rollbackTransaction();
              iJDebug::setError('CRITICAL ERROR: UPDATE TABLE ' . TABLE_GAME_SERVERS);
              return;
            }
          }
          $iJDataBase->commitTransaction();
        }                                      
    }
?>
